package com.ctsi.offlinesupport.service;

import com.ctsi.security.ISecurity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class FlowSecuritySerivice {

    private static final Logger logger = LoggerFactory.getLogger(FlowSecuritySerivice.class);
    @Autowired
    private ISecurity iSecurity;

    public static final String SHENG_ROLE = "sheng-supportflow-dot";
    public static final String DEPART_ROLE = "depart-supportflow-dot";
    public static final String CITY_ROLE = "dishi-supportflow-dot";

    /**
     * 判断是否控制权限
     * @param userLoginId
     * @return
     */
    public boolean isControl(String userLoginId){
        boolean sheng = iSecurity.isControl(userLoginId,"organChain","SELECT");
     //   boolean depart = iSecurity.isControl(userLoginId,"depart-dataObjectIds","SELECT");
     //   boolean ctity = iSecurity.isControl(userLoginId,"dishi-dataObjectIds","SELECT");
        if (sheng) {
            return true;
        }
        return false;
    }

    public Set<String> findDataObjectIds(String userLoginId){
        long begin = System.currentTimeMillis();
        List<String> roles = iSecurity.getSecurityRoles(userLoginId);
        Set<String> dataObjectIds = new HashSet<>();
        /**
         * 省角色
         */
        if (roles.contains(SHENG_ROLE)) {
            dataObjectIds.addAll(iSecurity.findDataObjectIds(userLoginId,"organChain","SELECT"));
        }
        /**
         * 部门角色
         */
        if (roles.contains(DEPART_ROLE)) {
            dataObjectIds.addAll(iSecurity.findDataObjectIds(userLoginId,"depart-dataObjectIds","SELECT"));
        }
        /**
         * 地市角色
         */
        if (roles.contains(CITY_ROLE)) {
            dataObjectIds.addAll(iSecurity.findDataObjectIds(userLoginId,"dishi-dataObjectIds","SELECT"));
        }
        long end = System.currentTimeMillis();
        logger.warn("过滤支撑单权限耗时: {}",end - begin);
        return dataObjectIds;
    }


}
